from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, \
    QLineEdit, QProgressBar
from PyQt5.QtCore import Qt
from TransferTools.appfunction import receive_file
import threading

class ReceiveWindow(QWidget):
    def __init__(self, controller):
        super(ReceiveWindow, self).__init__()

        self.controller = controller
        self.container = QVBoxLayout()
        self.back_button = QPushButton("返回")
        self.ip_input = QLineEdit()
        self.ip_input.setPlaceholderText("请输入文件发送方ip")
        self.certain_button = QPushButton("确认")
        # 进度条相关
        self.process_view = QProgressBar()
        self.process_view.setRange(0, 100)
        self.process_view.setValue(0)
        self.status = [0]


        self.equip_view()
        self.function_init()



    def equip_view(self):
        self.setLayout(self.container)

        # 层级关系
        self.container.addStretch(1)
        self.container.addWidget(self.ip_input)
        self.container.addWidget(self.certain_button)
        self.container.addWidget(self.back_button)
        self.container.addWidget(self.process_view)

        self.container.addStretch(1)

        # 显示风格
        self.process_view.setAlignment(Qt.AlignCenter)
        self.process_view.hide()

    def function_init(self):
        self.back_button.clicked.connect(self.back_button_func)
        self.certain_button.clicked.connect(self.certain_button_func)

    def back_button_func(self):
        self.controller.change_view(0)

    def processing_view(self):
        value = self.status[0]
        self.process_view.setValue(value)
        return value

    def certain_button_func(self):
        # 获取输入框的IP
        ip = self.ip_input.text()
        if not ip:  return
        print("准备接收文件")
        self.process_view.show()
        self.process_view.setValue(0)
        receive_thread = threading.Thread(target=receive_file, args=(ip, self))
        receive_thread.start()